﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Squog_1._1.Managers;

namespace Squog_1._1
{
    public class DrawableObject : GameObject
    {
        struct AnimationSequence
        {
            public int StartColumn;
            public int EndColumn;
            public int StartRow;
            public int EndRow;
            public int AnimationDelay;
            public bool LoopBack;
        }

        private int nextSwitchTime = 0;
        protected bool DoDraw = true;
        private Dictionary<String, AnimationSequence> Animations;
        private AnimationSequence CurrentAnimantion;

        private SpriteSheet spritesheet;
        public SpriteSheet spriteSheet { get { return spritesheet; } private set { spritesheet = value; } }
        private int CurrentRow;
        private int CurrentColumn;

        public Rectangle SourceRectangle { get { return new Rectangle(CurrentColumn * spriteSheet.Width, CurrentRow * spriteSheet.Height, spriteSheet.Width, spriteSheet.Height); } }
        public SpriteEffects DrawEffects { get; set; }
        public float Scale { get; protected set; }

        public bool CameraIndependent { get; private set; }
        public float Depth { get; set; }

        public bool LtoR = true;

        public DrawableObject(SpriteSheet spriteSheet)
        {
            this.spriteSheet = spriteSheet;

            Animations = new Dictionary<string, AnimationSequence>();

            AddAnimationSequence("default", 0, spriteSheet.Columns - 1, 0, spriteSheet.Rows - 1);
            ChangeAnimationSequence("default");

            CameraIndependent = false;
            Scale = 1;
        }

        public void AddAnimationSequence(String name, int StartColumn, int EndColumn, int StartRow, int EndRow)
        {
            AddAnimationSequence(name, StartColumn, EndColumn, StartRow, EndRow, 300, false);
        }
        public void AddAnimationSequence(String name, int StartColumn, int EndColumn, int StartRow, int EndRow, int AnimationDelay)
        {
            AddAnimationSequence(name, StartColumn, EndColumn, StartRow, EndRow, AnimationDelay, false);
        }
        public void AddAnimationSequence(String name, int StartColumn, int EndColumn, int StartRow, int EndRow, bool LoopBack)
        {
            AddAnimationSequence(name, StartColumn, EndColumn, StartRow, EndRow, 300, LoopBack);
        }
        public void AddAnimationSequence(String name, int StartColumn, int EndColumn, int StartRow, int EndRow, int AnimationDelay, bool LoopBack)
        {
            AnimationSequence newAnimation = new AnimationSequence();
            newAnimation.StartColumn = StartColumn;
            newAnimation.EndColumn = EndColumn;
            newAnimation.StartRow = StartRow;
            newAnimation.EndRow = EndRow;
            newAnimation.AnimationDelay = AnimationDelay;
            newAnimation.LoopBack = LoopBack;
            Animations.Add(name, newAnimation);
        }

        public void ChangeAnimationSequence(String name)
        {
            if (!Animations.TryGetValue(name, out CurrentAnimantion))
                throw new Exception("Animation " + name + " not found for " + this.ToString());
        }

        public void AnimationUpdate(GameTime gameTime)
        {
            if (LtoR == true)
            {
                nextSwitchTime -= gameTime.ElapsedGameTime.Milliseconds;
                if (nextSwitchTime <= 0)
                {
                    nextSwitchTime += CurrentAnimantion.AnimationDelay;

                    CurrentColumn++;
                    if (CurrentColumn > CurrentAnimantion.EndColumn)
                    {
                        CurrentColumn = CurrentAnimantion.StartColumn;
                        CurrentRow++;
                        if (CurrentRow > CurrentAnimantion.EndRow)
                        {
                            if (CurrentAnimantion.LoopBack)
                            {
                                CurrentRow = CurrentAnimantion.EndRow;
                                CurrentColumn = CurrentAnimantion.EndColumn;
                                LtoR = false;
                            }
                            else
                            {
                                CurrentRow = CurrentAnimantion.StartRow;
                            }
                        }
                    }
                }
            }
            else
            {
                nextSwitchTime -= gameTime.ElapsedGameTime.Milliseconds;
                if (nextSwitchTime <= 0)
                {
                    nextSwitchTime += CurrentAnimantion.AnimationDelay;

                    CurrentColumn--;
                    if (CurrentColumn < CurrentAnimantion.StartColumn)
                    {
                        CurrentColumn = CurrentAnimantion.EndColumn;
                        CurrentRow--;
                        if (CurrentRow < CurrentAnimantion.StartRow)
                        {
                            CurrentRow = CurrentAnimantion.StartRow;
                            CurrentColumn = CurrentAnimantion.StartColumn;
                            LtoR = true;
                        }
                    }
                }
            }

        }

        public virtual void Draw(Vector2 ScreenPosition)
        {
            if (DoDraw)
                DrawManager.spriteBatch.Draw(spriteSheet.Texture, ScreenPosition, SourceRectangle, Color.White, 0, Vector2.Zero, Scale, DrawEffects, 0.5f);
        }
    }
}
